"""
flask路由
"""

from flask import Flask, request  # 需要引入request--->带参数的路由
import readDem


application = Flask(__name__)


@application.route('/')
def hello():
    return 'hello'


@application.route('/getElevation', methods=['GET'])
def getElevationByLonLat():
    lon = request.args.get('lon')
    lat = request.args.get('lat')
    lonFlo = float(request.args.get('lonNum'))
    latFlo = float(request.args.get('latNum'))
    lonInt = int(lonFlo)
    latInt = int(latFlo)
    fileName = '{}{}{}{}'.format(lat, latInt, lon, lonInt)
    tifUrl = './tif/ASTGTMV003_{}_dem.tif'.format(fileName)
    try:
        res = readDem.getValByLonLat(tifUrl, lonFlo, latFlo)
    except Exception as e:
        return {
            'val': 'val is not exist'
        }
    return res


@application.route('/getElevationByPost', methods=['POST'])
def getElevationByLonLatByPost():
    lon = request.json.get('lon')
    lat = request.json.get('lat')
    lonFlo = float(request.json.get('lonNum'))
    latFlo = float(request.json.get('latNum'))
    lonFlo = float(request.json['lonNum'])
    latFlo = float(request.json['latNum'])
    lonInt = int(lonFlo)
    latInt = int(latFlo)
    fileName = '{}{}{}{}'.format(lat, latInt, lon, lonInt)
    tifUrl = './tif/ASTGTMV003_{}_dem.tif'.format(fileName)
    try:
        res = readDem.getValByLonLat(tifUrl, lonFlo, latFlo)
    except Exception as e:
        return {
            'val': 'val is not exist'
        }
    return res


def getLonLat(lon_lat):
    '''
    lon_lat:字符串,E119.6N36.4格式,暂时一位小数
    '''
    lon = lon_lat[0:1]
    lonFlo = float(lon_lat[1:6])
    lonInt = int(lonFlo)
    lat = lon_lat[6:7]
    latFlo = float(lon_lat[7:11])
    latInt = int(latFlo)
    return {
        'lon': lon,
        'lonFlo': lonFlo,
        'lonInt': lonInt,
        'lat': lat,
        'latFlo': latFlo,
        'latInt': latInt
    }


if __name__ == '__main__':
    # falsk热更新
    application.DEBUG = True
    application.jinja_env.auto_reload = True
    application.run(debug=True, host='0.0.0.0', port=4000)
    # application.run(host='0.0.0.0')
